package com.yuanfengerp.retailback.servicer.impl;

import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.retailback.dao.IRetailBackDao;
import com.yuanfengerp.retailback.model.RetailBackBoundOrder;
import com.yuanfengerp.retailback.servicer.IRetailBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class IRetailBackServiceImpl implements IRetailBackService {
    @Autowired
    private IRetailBackDao retailBackDao;

    @Override
    public Result<RetailBackBoundOrder> insertBackOrder(RetailBackBoundOrder retailBackBoundOrder) {
        int i = retailBackDao.insertBackOrder(retailBackBoundOrder);
        if (i > 0) {
            return Result.success(retailBackBoundOrder);
        } else {
            return Result.failure(500, "添加退货单失败");
        }
    }

    @Override
    public Result<RetailBackBoundOrder> updateBackOrderAgree(String[] retailReturnOrderNumbers) {
        for (String retailReturnOrderNumber : retailReturnOrderNumbers) {
            int status = retailBackDao.selectBackOrderStatus(retailReturnOrderNumber);
            if (status == 1 || status == 2) {
                return Result.failure(400, "当前订单状态为" + status + "，无法进行审核操作");
            } else {
                int updateNum = retailBackDao.updateBackOrderAgree(retailReturnOrderNumber);
                if (updateNum <= 0) {
                    return Result.failure(500, "退货单审核失败");
                }
            }
        }
        return Result.success();
    }

    @Override
    public Result<RetailBackBoundOrder> updateBackOrderRefuse(String[] retailReturnOrderNumbers) {
        int successNum = 0;
        int failNum = 0 ;
        for (String retailReturnOrderNumber : retailReturnOrderNumbers) {
            int status = retailBackDao.selectBackOrderStatus(retailReturnOrderNumber);
            if (status == 1 || status == 2) {
                failNum ++ ;
            } else {
                int updateNum = retailBackDao.updateBackOrderAgree(retailReturnOrderNumber);
                if (updateNum > 0) {
                    successNum++;
                } else {
                    return Result.failure(500, "退货单审核失败");
                }
            }
        }
        return Result.success();
    }

    @Override
    public Result<List<String>> selectAllOutOrder(Map<String, Object> condition) {
        List<RetailBackBoundOrder> retailBackBoundOrders = retailBackDao.selectAllBackOrder(condition);
        if (!retailBackBoundOrders.isEmpty()) {
            return Result.success();
        } else {
            return Result.failure(400, "没有相关数据");
        }
    }

    @Override
    public Result<RetailBackBoundOrder> selectBackOrder(String retailReturnOrderNumber) {
        RetailBackBoundOrder retailBackBoundOrder = retailBackDao.selectBackOrder(retailReturnOrderNumber);
        if (retailBackBoundOrder != null) {
            return Result.success(retailBackBoundOrder);
        } else {
            return Result.failure(400, "没有相关数据");
        }
    }
}
